package com.xwrite.toolkit;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.terminal.JBTerminalWidget;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentManager;
import org.jetbrains.plugins.terminal.ShellTerminalWidget;
import org.jetbrains.plugins.terminal.TerminalToolWindowFactory;
import org.jetbrains.plugins.terminal.TerminalView;

import java.io.File;
import java.io.IOException;
import java.util.Objects;

public class BuildRunnerAction extends AnAction {
    private static final String TAB_NAME = "build_runner";

    @Override
    public void actionPerformed(AnActionEvent event) {
        try {
            Project project = event.getProject();
            if(project == null){
                System.out.println("project is null");
                return;
            }
            VirtualFile data = event.getData(CommonDataKeys.VIRTUAL_FILE);
            if(data == null){
                System.out.println("target is not file or directory");
                return;
            }
            String target = "";
            if(data.isDirectory()){
                target = data.getPath() + "/*.dart";
            }else if(Objects.equals(data.getExtension(), "dart")){
                target = data.getParent().getPath() + "/" + data.getNameWithoutExtension() + "*.dart";
            }else{
                System.out.println("target file is not dart file, " + data.getPath());
                return;
            }
            target = target.replaceFirst(project.getBasePath() + "/", "");

            AppSettingsState settings = new AppSettingsStorage(project).get();
            String command = "dart";
            if(!settings.flutterSdkPath.isEmpty()){
                command = settings.flutterSdkPath + "/bin/dart";
            }
            command += " run build_runner build";
            if(settings.isDeleteConflictingOutputs){
                command += " --delete-conflicting-outputs";
            }
            command += " --build-filter=\""+target+"\"";
            executeCommand(project, command);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    private void executeCommand(Project project, String command) throws IOException {
        TerminalView terminalView = TerminalView.getInstance(project);
        ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow(TerminalToolWindowFactory.TOOL_WINDOW_ID);
        if(toolWindow == null){
            System.out.println("toolWindow is null");
            return;
        }
        ContentManager contentManager = toolWindow.getContentManager();
        Content content = contentManager.findContent(TAB_NAME);
        ShellTerminalWidget shellTerminalWidget;
        if(content == null){
            shellTerminalWidget = terminalView.createLocalShellWidget(project.getBasePath(), TAB_NAME);
        }else{
            shellTerminalWidget = (ShellTerminalWidget) TerminalView.getWidgetByContent(content);
        }
        assert shellTerminalWidget != null;
        shellTerminalWidget.executeCommand(command);
    }
}
